package leetcode.offer;

/**
 * ClassName: TranslateNumber
 * Description: leetcode-剑指offer 46.把数字翻译成字符串
 * date: 2021-11-08 21:34
 *
 * @author liyifan
 */
public class TranslateNumber {
    public int translateNum(int num) {
        String s = String.valueOf(num);
        int a = 1;
        int b = 1;
        for (int i = 2;i <= s.length();i++) {
            String temp = s.substring(i - 2, i);
            int c = temp.compareTo("10") >= 0 && temp.compareTo("25") <= 0 ? a + b : a;
            b = a;
            a = c;
        }
        return a;
    }

    public int translateNum2(int num) {
        String s = String.valueOf(num);
        int[] dp = new int[s.length() + 1];
        dp[0] = 1;
        dp[1] = 1;
        for (int i = 2;i <= s.length();i++) {
            String temp = s.substring(i - 2, i);
            if (temp.compareTo("10") >= 0 && temp.compareTo("25") <= 0) {
                dp[i] = dp[i - 1] + dp[i - 2];
            } else {
                dp[i] = dp[i - 1];
            }
        }
        return dp[s.length()];
    }
}
